package com.cdshangpai.utils.log;

import android.content.Context;
import android.content.SharedPreferences;

import com.cdshangpai.PhotoappApplication;

import java.text.SimpleDateFormat;
import java.util.Date;

public class LogUtil {
    private static String cacheKey="photoapp_log";
    private static String logTag="log";
    private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static SharedPreferences getStorage()
    {
        return PhotoappApplication.context.getSharedPreferences(cacheKey, Context.MODE_PRIVATE);
    }

    private static String nowTime()
    {
        Date now=new Date(System.currentTimeMillis());
        return sdf.format(now);
    }

    public static void save(String content)
    {
        SharedPreferences sp= getStorage();
        sp.edit().putString(logTag,load()+nowTime()+" "+content+"\n").commit();
    }

    public static String load()
    {
        SharedPreferences sp= getStorage();
        String log=sp.getString(logTag,"");
        return log;
    }

    public static void clean()
    {
        SharedPreferences sp= getStorage();
        sp.edit().remove(logTag).commit();
    }


}
